package com.warm.pump.module.work.service.impl;

import com.alibaba.fastjson.JSON;
import com.warm.pump.executor.InstanceService;
import com.warm.pump.frame.db.DbUtil;
import com.warm.pump.frame.gen.util.TypeCast;
import com.warm.pump.module.work.bean.po.gen.Work;
import com.warm.pump.module.work.dao.gen.WorkDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.warm.pump.module.work.dao.gen.WorkParamsDao;
import com.warm.pump.module.work.bean.po.gen.WorkParams;
import com.warm.pump.module.work.service.IWorkParamsService;
import com.warm.pump.frame.gen.bean.PageBean;



@Service("WorkParamsService")
public class WorkParamsServiceImpl implements IWorkParamsService {
	@Autowired
	private WorkParamsDao workParamsDao;
	@Autowired
	private InstanceService instanceService;
	@Autowired
	private WorkDao workDao;

	@Override
	public WorkParams queryObject(Long  id){
		return workParamsDao.selectByPrimaryKey( id);
	}

	public WorkParams queryObject(Map<String, Object> map){
		return workParamsDao.selectByMap( map);
	}
	
	@Override
	public List<WorkParams> queryList(Map<String, Object> map){
		return workParamsDao.getListByMap(map);
	}

    @Override
    public PageBean getPageBeanByMap(Map map,int pageNo,int pageSize) {
		return workParamsDao.getPageBeanByMap(map, pageNo, pageSize);
    }


	@Override
	public int queryTotal(Map<String, Object> map){
		return workParamsDao.countTotalByMap(map);
	}
	
	@Override
	public void save(WorkParams workParams){
		workParamsDao.insertSelective(workParams);
	}
	
	@Override
	public void update(WorkParams workParams){
		workParamsDao.updateByPrimaryKeySelective(workParams);
	}
	
	@Override
	public void delete(Long  id){
		workParamsDao.deleteByPrimaryKey(id);
	}

	@Override
	public void delete(Map<String, Object>  map){
	workParamsDao.deleteByMap(map);
	}
	
	@Override
	public void deleteBatch(Long[]  ids){
		Map<String, Object> map = new HashMap<>();
		map.put("id_in",ids);
		workParamsDao.deleteByMap(map);
	}

	/**
	 * 参数管理中关联作业创建实例
	 * @param workId
	 * @param paramsId
	 */
	public void createInstByWorkIdAndParamsId(Long workId, Long paramsId) {
		Work work = workDao.selectByPrimaryKey(workId);
		WorkParams workParams = workParamsDao.selectByPrimaryKey(paramsId);


		String instId = instanceService.createInst("workAndParams", workId, work.getWorkName(), 1, "参数管理中关联作业执行，参数id为" + paramsId);

		Map<String, Object> rtMap = new HashMap<>();
		Integer paramsType = workParams.getType();
		if (paramsType == 3) {
			List<Map<String, Object>> list = DbUtil.findList(workParams.getDbKey(), workParams.getParamValue());
			for (Map<String, Object> map : list) {
				for(Map.Entry<String,Object> entry:map.entrySet()){
					map.put(entry.getKey(),TypeCast.getObjectWithDateFormat(entry.getValue()));
				}
				String  paramsStr = JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd HH:mm:ss");
				instanceService.createInstDtl(instId,work.getId(),work.getWorkName(),null,null,paramsStr);
			}
			return;
		}else if (paramsType == 2){
			Map<String, Object> ones = DbUtil.findOne(workParams.getDbKey(), workParams.getParamValue());
			for(Map.Entry<String,Object> one : ones.entrySet()){
				rtMap.put(one.getKey(), TypeCast.getObjectWithDateFormat(one.getValue()));
			}
		}else if (paramsType == 1){
			rtMap.put(workParams.getParamKey(), TypeCast.getObjectWithDateFormat(workParams.getParamValue()));
		}
		String paramsStr = JSON.toJSONStringWithDateFormat(rtMap,"yyyy-MM-dd HH:mm:ss");
		instanceService.createInstDtl(instId,work.getId(),work.getWorkName(),null,null,paramsStr);
	}


}
